//
//  DatePickerViewController.m
//  IWeighMyself
//
//  Created by Francisco Gindre on 8/21/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DatePickerViewController.h"


@implementation DatePickerViewController
@synthesize doneButton;
@synthesize datePicker;
@synthesize tableView;
@synthesize newWeightView;
@synthesize dateCell;



-(id)initWithParentView:(LogNewWeightViewController*)newWeightController aDate:(NSDate*)date {
	self = [[DatePickerViewController alloc]initWithNibName:@"DatePickerViewController" bundle:nil];
	
	if (self!=nil){
		// init the title
		self.title = NSLocalizedString(@"DatePickerTitle",@"");
		// set the view controller that receives the date
		self.newWeightView = [newWeightController retain];
		//set the actual date to display when view shows up
		self.dateCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] retain];
		[dateCell.textLabel setText:[NSDateFormatter localizedStringFromDate:newWeightController.currentEntry.dateLogged
						  dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]];
		
		[self.datePicker setDate:newWeightController.currentEntry.dateLogged];
	}
	return self;
}
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	//create the done button to be displayed if changes occur
	self.doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DoneButtonTitle",@"") style:UIBarButtonItemStyleDone target:self action:@selector(doneAction:)];
	
	[self.doneButton retain];
	
	[dateCell.textLabel setText:[NSDateFormatter localizedStringFromDate:newWeightView.currentEntry.dateLogged
															   dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]];
	
	[self.datePicker setDate:newWeightView.currentEntry.dateLogged];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.datePicker = nil;
	self.dateCell = nil;
	
//	NSLog(@"viewDidUnLoad");
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark datePicker events

/*
 handles an event dispatched by date picker when flicked
 */

-(IBAction)datePickerFlicked:(id)sender {
//	NSLog(@"date flicked %@",[datePicker date]);
	[self.dateCell.textLabel setText:[NSDateFormatter localizedStringFromDate:[datePicker date]
									dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]];

	if (self.navigationItem.rightBarButtonItem ==nil) {
		
		
		self.navigationItem.rightBarButtonItem = [self.doneButton retain];
	}
	
	
		
}
#pragma mark -
#pragma mark DoneButton action handling

-(void)doneAction:(id)sender {
	
	newWeightView.currentEntry.dateLogged = [[datePicker date] retain];
	
	[newWeightView.tableView  reloadData];
	[self.navigationController popViewControllerAnimated:YES];
	
	
	
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
	 
	 if (dateCell == nil) {
	 dateCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
	 }
	 */
    // Configure the cell...
	[dateCell.textLabel setText:[NSDateFormatter localizedStringFromDate:[datePicker date]
															   dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]];

		
    return dateCell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return NSLocalizedString(@"DatePickerTitle",@"");
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
}


@end
